const sessionCache = {
  set(key, value) {
    if (!key) return

    sessionStorage.setItem(key, JSON.stringify(value ?? ''))
  },
  get(key) {
    if (!key) return null

    let value = sessionStorage.getItem(key)
    try {
      value = JSON.parse(value)
    } catch (error) {
      console.log(error)
    }

    return value ?? null
  },
  remove(key) {
    sessionStorage.removeItem(key)
  },
  clear() {
    sessionStorage.clear()
  },
}

const localCache = {
  set(key, value) {
    if (!key) return

    localStorage.setItem(key, JSON.stringify(value ?? ''))
  },
  get(key) {
    if (!key) return null

    let value = localStorage.getItem(key)
    try {
      value = JSON.parse(value)
    } catch (error) {
      console.log(error)
    }

    return value ?? null
  },
  remove(key) {
    localStorage.removeItem(key)
  },
  clear() {
    localStorage.clear()
  },
}

export default {
  session: sessionCache,
  local: localCache,
}
